package com.android.topview.watermelon.exchange

import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.topview.watermelon.R
import com.android.topview.watermelon.base.BaseActivity
import com.android.topview.watermelon.util.setTalkBackFocus
import kotlinx.android.synthetic.main.activity_exchange.*

class ExchangeActivity : BaseActivity() {

    private lateinit var title: TextView

    private lateinit var back: ImageView
    private lateinit var ok: ImageView

    private lateinit var fromText: TextView
    private lateinit var toText: TextView

    private lateinit var fromList: RecyclerView
    private lateinit var toList: RecyclerView

    private lateinit var fromAdapter: ExchangeListAdapter
    private lateinit var toAdapter: ExchangeListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_exchange)
        init()
    }

    private fun init() {
        fromText = tv_exchange_from
        toText = tv_exchange_to
        back = iv_exchange_back
        ok = iv_exchange_ok
        fromList = rv_exchange_from_list
        toList = rv_exchange_to_list
        title = tv_exchange_title

        fromText.contentDescription =
            "${getString(R.string.select_exchange_from)}，列表共${EXCHANGE_CODE_ARRAY.size}项"
        toText.contentDescription =
            "${getString(R.string.select_exchange_to)}，列表共${EXCHANGE_CODE_ARRAY.size}项"

        back.setOnClickListener {
            finish()
        }

        ok.setOnClickListener {
            startActivity(
                Intent(this, ExchangeResultActivity::class.java).apply {
                    putExtra("from", fromAdapter.selectIndex)
                    putExtra("to", toAdapter.selectIndex)
                }
            )
        }

        fromAdapter = ExchangeListAdapter(ExchangeCode.CNY)
        toAdapter = ExchangeListAdapter(ExchangeCode.USD)
        fromList.layoutManager = LinearLayoutManager(this)
        toList.layoutManager = LinearLayoutManager(this)
        fromList.adapter = fromAdapter
        toList.adapter = toAdapter

        title.setTalkBackFocus()
    }

}